<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="code">
        <el-input
            v-model="queryParams.code"
            placeholder="请输入姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="部门" prop="orgid">
        <treeselect v-model="queryParams.orgid" :options="deptOptions" :show-count="true" placeholder="请选择归属部门"
                    style="width: 200px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" width="120"/>
      <el-table-column label="性别" align="center" prop="gender" width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="工号" align="center" prop="code" width="120" />
      <el-table-column label="部门" align="center" prop="orgName"/>
      <el-table-column label="民族" align="center" prop="ethnicgroup" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_ethnic_group" :value="scope.row.ethnicgroup"/>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phonenumber" width="130"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="openInfo(scope.row,false)"
              v-hasPermi="['business:person:edit']"
          >档案维护
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="openInfo(scope.row,true)"
              v-hasPermi="['business:person:remove']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改人员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-collapse v-model="activeNames">
          <el-collapse-item name="personInfo">
            <template v-slot:title>
              <span style="font: 18px bold">人员信息</span>
            </template>
            <el-row>
              <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="form.name" placeholder="请输入姓名" :disabled="true"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="性别">
                  <el-radio-group v-model="form.gender" :disabled="true">
                    <el-radio
                        v-for="dict in genderOptions"
                        :key="dict.dictValue"
                        :label="dict.dictValue"
                    >{{ dict.dictLabel }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="年龄" prop="age">
                  <el-input v-model="form.age" placeholder="请输入年龄" :disabled="true"/>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="部门" prop="orgid">
                  <!--          <el-select v-model="form.orgid" placeholder="请选择部门">-->
                  <!--            <el-option label="请选择字典生成" value="" />-->
                  <treeselect v-model="form.orgid" :options="deptOptions" :show-count="true"
                              placeholder="请选择归属部门" :disabled="true"/>
                  <!--          </el-select>-->
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="民族" prop="ethnicgroup">
                  <el-select v-model="form.ethnicgroup" placeholder="请选择民族" style="width: 100%;" :disabled="true">
                    <el-option
                        v-for="dict in ethnicgroupOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号" prop="identity">
                  <el-input v-model="form.identity" placeholder="请输入身份证号" :disabled="true"/>
                </el-form-item>

              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="手机号" prop="phonenumber">
                  <el-input v-model="form.phonenumber" placeholder="请输入手机号" :disabled="true"/>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="form.email" placeholder="请输入邮箱" :disabled="true"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="学历" prop="education">
                  <el-select v-model="form.education" placeholder="请选择学历" style="width: 100%;" :disabled="true">
                    <el-option
                        v-for="dict in educationOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="出生日期" prop="birthday">
                  <el-date-picker clearable size="small" style="width: 100%" :disabled="true"
                                  v-model="form.birthday"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择出生日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="职务" prop="office">
                  <el-select v-model="form.office" placeholder="请选择职务" style="width: 100%;" :disabled="true">
                    <el-option
                        v-for="dict in offices"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="职称" prop="jobTitle">
                  <el-select v-model="form.jobTitle" placeholder="请选择职称" style="width: 100%;" :disabled="true">
                    <el-option
                        v-for="dict in jobs"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="政治面貌" prop="politicaloutlook">
                  <el-select v-model="form.politicaloutlook" placeholder="请选择政治面貌" style="width: 100%;"
                             :disabled="true">
                    <el-option
                        v-for="dict in politicaloutlookOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>

              </el-col>
              <el-col :span="12">
                <el-form-item label="密级" prop="secretLevel">
                  <el-select v-model="form.secretLevel" placeholder="请选择密级" style="width: 100%;" :disabled="true">
                    <el-option
                        v-for="dict in secretLevel"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>

              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="档案位置" prop="archiveLocation">
                  <el-input v-model="form.archiveLocation" type="textarea" placeholder="请输入内容" :disabled="true"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="住址" prop="addr">
                  <el-input v-model="form.addr" type="textarea" placeholder="请输入内容" :disabled="true"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <el-collapse-item :name="docType.name" v-for="(docType,index) in docDirectory">
            <template #title>
              <span style="font: 18px bold">{{ index + 1 }}、{{ docType.name }}</span>
            </template>
            <template #default>
              <el-table :data="docType.children" style="width: 100%">
                <el-table-column prop="name" label="名称" width="250"/>
                <el-table-column prop="updateTime" label="材料形成时间">
                  <template #default="scope">
                    <span>{{ parseTime(scope.row.fileFromTime, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="updateTime" label="更新时间">
                  <template #default="scope">
                    <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="status" label="状态"/>
                <el-table-column prop="option" label="操作" width="120">
                  <template #default="scope">
                    <el-button v-if="!isView"
                               size="mini"
                               type="text"
                               icon="el-icon-edit"
                               @click="openScanner(scope.row,false)"
                               v-hasPermi="['business:person:edit']"
                    >档案维护
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="openScanner(scope.row,true)"
                        v-hasPermi="['business:person:remove']"
                    >查看
                    </el-button>
                  </template>

                </el-table-column>
              </el-table>
            </template>
          </el-collapse-item>
        </el-collapse>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <file-scanner ref="scanner"/>
    </el-dialog>
  </div>
</template>

<script>
import {addPerson, delPerson, exportPerson, getPerson, listPerson, updatePerson} from "@/api/business/person";

import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {topAndLeaf} from "@/api/business/directory";
import {parseTime} from "@/utils/ruoyi";
import FileScanner from "@/views/business/person/scanner.vue";

export default {
  name: "Person",
  components: {FileScanner, Treeselect},
  dicts: ['sys_user_sex', 'sys_ethnic_group', 'sys_posts', 'sys_job', 'secret_level'],
  data() {
    return {
      activeNames: ['personInfo'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      // 人员管理表格数据
      personList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isView: false,
      // 性别字典
      genderOptions: [],
      // 民族字典
      ethnicgroupOptions: [],
      // 学历字典
      educationOptions: [],
      // 政治面貌字典
      politicaloutlookOptions: [],
      //职务
      offices: [],
      //职称
      jobs: [],
      //密级
      secretLevel: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        gender: null,
        orgid: null,
        office: null,
        jobTitle: null,
        archiveLocation: null,
        secretLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
      },
      docDirectory: [],
      scanner: {
        show: false,
        type: null,
        topType: null,
        personId: null
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.genderOptions = response.data;
    });
    this.getDicts("sys_ethnic_group").then(response => {
      this.ethnicgroupOptions = response.data;
    });
    this.getDicts("sys_degree").then(response => {
      this.educationOptions = response.data;
    });
    this.getDicts("sys_political_outlook").then(response => {
      this.politicaloutlookOptions = response.data;
    });
    this.getDicts("sys_posts").then(response => {
      this.offices = response.data;
    });
    this.getDicts("sys_job").then(response => {
      this.jobs = response.data;
    });
    this.getDicts("secret_level").then(response => {
      this.secretLevel = response.data;
    });

  },
  methods: {
    parseTime,
    /** 查询人员管理列表 */
    getList() {
      this.loading = true;
      this.getTreeselect();
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    genderFormat(row, column) {
      return this.selectDictLabel(this.genderOptions, row.gender);
    },
    // 民族字典翻译
    ethnicgroupFormat(row, column) {
      return this.selectDictLabel(this.ethnicgroupOptions, row.ethnicgroup);
    },
    // 学历字典翻译
    educationFormat(row, column) {
      return this.selectDictLabel(this.educationOptions, row.education);
    },
    // 政治面貌字典翻译
    politicaloutlookFormat(row, column) {
      return this.selectDictLabel(this.politicaloutlookOptions, row.politicaloutlook);
    },
    officeFormat(row, column) {
      return this.selectDictLabel(this.offices, row.office);
    },
    jobFormat(row, column) {
      return this.selectDictLabel(this.jobs, row.office);
    },
    secretLevelFormat(row, column) {
      return this.selectDictLabel(this.secretLevel, row.office);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        gender: "0",
        age: null,
        orgid: null,
        ethnicgroup: null,
        addr: null,
        phonenumber: null,
        email: null,
        education: null,
        birthday: null,
        office: null,
        jobTitle: null,
        politicaloutlook: null,
        archiveLocation: null,
        identity: null,
        secretLevel: null,
        userid: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.activeNames = ['personInfo']

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加人员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids
      getPerson(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员管理";
      });
    },
    openInfo(row, isView) {
      this.isView = isView === undefined ? false : isView;
      if (this.isView) {
        this.title = "档案查看";
      } else {
        this.title = "档案维护";
      }
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids
      getPerson(id).then(response => {
        this.form = response.data;
        this.open = true;
        topAndLeaf(id).then(response => {
          this.docDirectory = response.data;
        })
      });
    },
    openScanner(row, isView) {
      this.$refs.scanner.open({
        fileTopType: row.parentId,
        fileType: row.id,
        personId: row.personId,
      }, {fileType: row.name, personName: this.form.name, isView})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePerson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除人员姓名为"' + row.name + '"的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delPerson(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人员管理数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportPerson(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgid = data.id;
      this.getList();
    }
  }
};
</script>
